var curSearch;
var orderStatus = "";
//订单列表实例
var $table = $("#refund_order_list_table");

$(function () {
    initOrdersTable($table);
});


//更换筛选订单状态
$("#orderStatus").on("change", function () {
    var temp = $("#orderStatus").val();
    orderStatus = temp;
    $table.bootstrapTable('refresh');
});


var columns = [
    {
        checkbox: true,
    }, {
        title: "序号",
        align: "center",
        valign: "middle",
        width: "50px",
        formatter: function (value, row, index) {
            return (index + 1);
        }
    }, {
        field: "id",
        title: "订单编号",
        halign: "center",
        valign: "middle",
        align: "center",
        width: "80px",
        formatter: function (value, row, index) {
            return row.order.orderNumber;
        }
    }, {
        field: "refundNumber",
        title: "售后订单编号",
        valign: "middle",
        halign: "center",
        sortable: true
    }, {
        field: "totalPrice",
        title: "实付金额",
        align: "center",
        valign: "middle",
        halign: "center",
        width: "40px",
        sortable: true,
        formatter: function (value, row, index) {
            return row.order.cashPayPrice;
        }
    }, {
        field: "refundMoney",
        title: "退款金额",
        halign: "center",
        valign: "middle",
        width: "50px",
        sortable: true

    }, {
        field: "muser",
        title: "买家姓名",
        halign: "center",
        valign: "middle",
        width: "80px",
        sortable: true,
        formatter: function (value, row, index) {
            var muserName = row.muser.userName;
            return muserName;
        }
    }, {
        field: "refundReason",
        title: "退款原因",
        align: "center",
        valign: "middle",
        halign: "center",
        sortable: true
    }, {
        field: "refundMessage",
        title: "退款说明",
        align: "center",
        valign: "middle",
        width: "70px",
        halign: "center",
        sortable: true
    }, {
        field: "pictureUrl",
        title: "退款凭证",
        align: "center",
        valign: "middle",
        width: "300px",
        halign: "center",
        sortable: true,
        formatter: function (value, row, index) {
            if (value != null && value != '') {
                var pics = value.split(",");
                var pic = "";
                for (var item of pics) {
                    pic += "<img style=\"width: 50px;height: 50px;\" src=\"" + item + "\">";
                }
                return pic
            } else {
                return "";
            }
        }
    }, {
        field: "payType",
        title: "退款方式",
        align: "center",
        valign: "middle",
        halign: "center",
        sortable: true,
        formatter: function (value, row, index) {
            var str = "";
            if (row.order.payType == 1) {
                str = "哆币";
            } else if (row.order.payType == 2) {
                str = "雅币";
            } else if (row.order.payType == 3) {
                str = "支付宝";
            } else if (row.order.payType == 4) {
                str = "微信";
            }
            return str;
        }

    }, {
        field: "payDatetime",
        title: "支付时间",
        align: "center",
        valign: "middle",
        halign: "center",
        sortable: true,
        formatter: function (value, row, index) {
            return row.order.payDatetime;
        }

    }, {
        field: "status",
        title: "订单状态",
        align: "center",
        valign: "middle",
        width: "70px",
        halign: "center",
        sortable: true,
        formatter: function (value, row, index) {
            return convertOrderStatus(value);
        }
    }, {
        field: "gmtDatetime",
        title: "申请退款时间",
        align: "center",
        valign: "middle",
        halign: "center",
        sortable: true
    }, {
        field: "uptDatetime",
        title: "处理时间",
        align: "center",
        valign: "middle",
        halign: "center",
        sortable: true
    }, {
        field: "status",
        title: "操作",
        halign: "center",
        valign: "middle",
        sortable: true,
        formatter: function (value, row, index) {
            var optHtml = "";
            if (value == 1) {
                optHtml += "<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"agreeRefund(" + row.id + ",1)\" data-toggle=\"modal\" data-target=\"#shippingModal\"> 同意</a>\n";
                optHtml += "<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"agreeRefund(" + row.id + ",2)\" data-toggle=\"modal\" data-target=\"#shippingModal\"> 拒绝</a>\n";
                return optHtml;
            } else if (value == 4) {
                optHtml += "<label  data-toggle=\"modal\" data-target=\"#shippingModal\"> 已退款</label>\n";
                return optHtml;
            } else if (value == 2) {
                optHtml += "<label  data-toggle=\"modal\" data-target=\"#shippingModal\">会员已取消</label>\n";
                return optHtml;
            } else if (value == 3) {
                optHtml += "<label  data-toggle=\"modal\" data-target=\"#shippingModal\">已拒绝</label>\n";
                return optHtml;
            }

        }
    }];

var queryParams = function (params) {
    console.log("params:", params);
    var param = {
        t: {
            status: orderStatus
        },
        page: {
            offset: params.offset,
            current: params.offset / params.limit + 1,//bootstrap-table分页参数offset从0开始，计算的后端当前页参数current=(offset/limit+1)，
            size: params.limit
        },
        searchText: params.search
    };

    return param;
}


//初始化表格 tableins:表格实例 ,
function initOrdersTable(tableins) {
    tableins.bootstrapTable({
        columns: columns,
        method: "post",
        url: urlcore + "/api/afterSaleOrder/selectAfterSaleOrders",
        dataType: "json",
        queryParams: function (params) {
            return queryParams(params);
        },
        //返回数据预处理
        responseHandler: function (cbdata) {
            if (null != cbdata.data && null != cbdata.data.records) {
                var records = cbdata.data.records;
                var res = {
                    //如果采用server端分页，必须有rows和total这两部分数据
                    "rows": records,
                    "total": cbdata.data.total
                };
                return res;
            } else return {"rows": [], "total": 0};
        },
        cache: false,
        uniqueId: "id",
        // toolbar: '#OrderListToolbar',//自定义工具栏，不同表格工具栏不同，一个jQuery 选择器，指明自定义的toolbar 例如:#toolbar, .toolbar.
        pagination: true,//启用分页条
        sidePagination: "server",//分页方式client或server
        // pageNumber: 1,
        pageSize: 16,//每页大小
        pageList: [16, 32, 48, 64],//可以选择每页大小
        showRefresh: true,//显示刷新
        sortable: true,//是否启用排序
        clickToSelect: true,//设置true 将在点击行时，自动选择rediobox 和 checkbox
        search: true,//默认为客户端搜索，服务器端接收params中的searchText后可以做服务器端搜索
        // searchText:"",//初始搜索文本
    });
}


//初始化模态框
function initModal() {
    $("#isLogisticsShowDiv").show();
    $("input[type='radio'][name='deliveryRadio'][value='1']").prop("checked", "checked");
    $("#modalGoodsList").html("");
    $("#modalDeliveryNum").val("");
    modalOrderId = "";
    modalIsLogistics = "";
}

// 修改订单，关闭订单
function agreeRefund(id, type) {
    if (type == 2) {
        confirm("确定拒绝该申请", function (callback) {
            if (callback) {
                doOperation();
            }
        });
    } else {
        doOperation();
    }

    function doOperation() {
        $.ajax({
            type: "get",
            url: urlcore + "/api/afterSaleOrder/agreeRefund?id=" + id + "&type=" + type,
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: {},
            success: function (data) {
                if (data.success == false) {
                    alert(data.msg)
                } else {
                    $table.bootstrapTable("refresh");
                }

            },
            error: function () {
                alert(data.msg)
            }
        });
    }
}


//转化订单状态 已完成和待评价都是交易完成 6-9都是交易关闭
function convertOrderStatus(value) {
    var result;
    if (value == null || value == undefined)
        return "";
    if (value == 1) {
        result = "申请退款中";
    } else if (value == 2) {
        result = "会员已取消";
    } else if (value == 3) {
        result = "商家已拒绝";
    } else if (value == 4) {
        result = "商家已退款";
    }
    return result;
}